package com.ybfenpan.controller.upload;


import java.io.PrintWriter;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import com.ybfenpan.controller.core.BaseController;
import com.ybfenpan.utils.FileUtils;
import com.ybfenpan.utils.SysConst;
import com.ybfenpan.utils.Tools;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;






/**
 * Created by Administrator on 2016/3/25.
 */
@Controller

public class ImgUploadController extends BaseController {
    
     
    @RequestMapping("upload/imgUpload.shtml")
    
    public void imgUpload(Map<String, Object> map, MultipartFile filedata, HttpServletRequest request, HttpServletResponse response) throws Exception {
    	response.setContentType("text/html; charset=UTF-8");
    	 
    	 
    	 
    	if(filedata!=null){
    		 System.out.println("上传操作");
    		 String fileName = filedata.getOriginalFilename();
         	 String docName =  FileUtils.getUUIDFileName(fileName);
         	//复制保存到E盘
             String newFile = SysConst.TMP_FILE_NOTICE + docName;
             
             
             String fileContentType=filedata.getContentType();
             //后缀名
 			String ext = fileName.substring(fileName.lastIndexOf('.')+1).toLowerCase();
 		
 			List<String> arrowType = Arrays.asList("images/gif","images/jpg","images/jpeg","images/pjpeg","images/png","images/bmp","image/gif","image/jpg","image/jpeg","image/pjpeg","image/png","image/bmp","image/x-png");
 			List<String> arrowExt = Arrays.asList("gif","jpg","jpeg","png","bmp");
 			PrintWriter out = response.getWriter();
 			 
 			if(!(arrowType.contains(fileContentType.toLowerCase()) && arrowExt.contains(ext))){
 				out.println("{\"err\":\"上传文件只接收gif/jpg/jpeg/png/bmp类型。\",\"msg\":\"\"}");// 这里返回你图片上传路径，返回json到编辑器中，这样编辑器就能及时显示图片内容了
   			  
 			}else{
 				
 				if(filedata.getSize()>(4*1024*1024)){
 					
 					
 					out.println("{\"err\":\"不能大于4M\",\"msg\":\"\"}");// 这里返回你图片上传路径，返回json到编辑器中，这样编辑器就能及时显示图片内容了
	     			  
 				}else{
 					//文件存储到硬盘
 	         		boolean flag = FileUtils.createFileToDisk(newFile, filedata.getInputStream());
 	 				 
 	                if(flag){
 	                	 String tmpFile = Tools.getWebPath(this.getClass())+SysConst.TMP_FILE_NOTICE+docName;
 	                     FileUtils.copyFile(newFile,tmpFile);
 	                     
 	                     String path = request.getContextPath();
 	                 	 String port = (0<request.getServerPort()) ? (":"+request.getServerPort()) : "";
 	                 	 String basePath = request.getScheme()+"://"+request.getServerName()+port+path+"/";
 	                 	 
 	        			 String pathName = basePath+SysConst.TMP_FILE_NOTICE+docName;
 	        			 
 	        			 System.out.println(pathName);

 	        			 out.println("{\"err\":\"\",\"msg\":\"" + pathName + "\"}");// 这里返回你图片上传路径，返回json到编辑器中，这样编辑器就能及时显示图片内容了
 	     			 
 	                     //return "redirect:mallInfoShow.shtml";
 	                 }else{
 	                 	 
 	                	
 	                	out.println("{\"err\":\"上传失败\",\"msg\":\"\"}");// 这里返回你图片上传路径，返回json到编辑器中，这样编辑器就能及时显示图片内容了
 		     			  
 	                 }
 				}
 				
                 
 			}
		}
    	 
    }

     
    
}
